package hlhj.fhp.tvlib.fragment

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.activitys.LiveAty
import hlhj.fhp.tvlib.javabean.LiveDetailBean
import kotlinx.android.synthetic.main.live_fgm_info.*
import kotlinx.android.synthetic.main.live_fgm_info.view.*

/**
 * Created by Administrator on 2018\5\24 0024.
 */
class LiveInfoFgm :Fragment() {
    private lateinit var rootView:View
    private var lid="";
    companion object {
        fun getInstence(id:String,type:Int): LiveInfoFgm {
            val infoFgm = LiveInfoFgm()
            val bundle = Bundle()
            bundle.putString("id",id)
            bundle.putInt("type",type)
            infoFgm.arguments=bundle
            return infoFgm
        }
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView=inflater.inflate(R.layout.live_fgm_info,null)
        lid=arguments?.getString("id")!!
        when(arguments?.getInt("type")){
            1->{//视频
                rootView.bottomBar.visibility=View.VISIBLE
            }
            2->{//图文
                rootView.bottomBar.visibility=View.GONE
            }
        }
        getDetailDetail()

        rootView.btSend.setOnClickListener {
            if (rootView.etContent.text.isEmpty())return@setOnClickListener
            val liveAty = activity!! as LiveAty
            liveAty.sendDM(etContent.text.toString())
            rootView.etContent.setText("")
            val inputManager = rootView.etContent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputManager.hideSoftInputFromWindow(rootView.etContent.windowToken,0)
        }

        return rootView

    }
    private fun getDetailDetail() {
        OkGo.get<String>(Contents.BaseUrl + Contents.LIVE_DETAIL)
                .params("live_id", lid)
                .execute(object :StringCallback(){
                    override fun onSuccess(response: Response<String>?) {
                        val str = response?.body()
                        val detailBean = Gson().fromJson(str, LiveDetailBean::class.java)
                        if (detailBean.code==1){
                            if (detailBean.data.vote_status==1){
                                rootView.bottomBar.visibility=View.VISIBLE
                            }else if (detailBean.data.vote_status==2){
                                rootView.bottomBar.visibility=View.GONE
                            }
                            rootView.tvTittle.text="${detailBean.data.live_title}"
                            rootView.tvInfo.text=detailBean.data.live_desc
                        }
                    }
                })
    }

    public fun onEditClick(){
        val inputManager = rootView.etContent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputManager.showSoftInput(etContent,0)
    }
}